package middleware

import (
	"net/http"
	"wms/util"

	"github.com/icetech233/gin"
)

func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "*")
		c.Header("Access-Control-Allow-Methods", "OPTIONS,POST,GET,DELETE,PUT")
		c.Header("Access-Control-Allow-Credentials", "false")

		// 放行所有OPTIONS方法
		if method == "OPTIONS" {
			// c.AbortWithStatus(http.StatusNoContent)
			c.AbortWithStatus(204)
		}
		// 处理请求
		c.Next()
	}
}

func LogRequest() gin.HandlerFunc {
	return func(c *gin.Context) {
		// GET*/api/v1/tenant/list*/api/v1/tenant/list?name=%E5%AE%89%E5%BE%BD
		// util.Log(c.Request.Method + "*" + c.Request.URL.Path + "*" + c.Request.RequestURI)
		util.Log(c.Request.Method + "|" + c.Request.RequestURI)
		// 处理请求
		c.Next()
	}
}

func Cors1(c *gin.Context) {
	method := c.Request.Method
	c.Header("Access-Control-Allow-Origin", "*")
	c.Header("Access-Control-Allow-Headers", "*")
	c.Header("Access-Control-Allow-Methods", "OPTIONS,POST,GET,DELETE,PUT")
	c.Header("Access-Control-Allow-Credentials", "false")

	// 放行所有OPTIONS方法
	if method == "OPTIONS" {
		// c.AbortWithStatus(http.StatusNoContent)
		c.AbortWithStatus(204)
	}
	// 处理请求
	c.Next()
}

func Cors22() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		c.Header("Access-Control-Allow-Origin", origin)
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id")
		c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")

		// 放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		// 处理请求
		c.Next()
	}
}
